library(doBy)

the_prefix <- ""

load_it <- 0
if (load_it>0) {
    load(
        paste0(
            the_prefix,"data/kff_main_replication_data_with_psraid.RData"
        )
    )
    load_it <- load_it+1
    }

sdat$INSURANCETYPE <- "Other"
sdat$INSURANCETYPE[sdat$MEDICAID==1] <- "Medicaid"
sdat$INSURANCETYPE[sdat$MEDICARESR==1] <- "Medicare"
sdat$INSURANCETYPE[sdat$EMPLINSURE==1] <- "Employer"
sdat$INSURANCETYPE[sdat$SELFINSURE==1] <- "Purchased"
sdat$INSURANCETYPE[sdat$COVERED==0] <- "None"

mean.na <- function(x){
	mean(x,na.rm=T)
}

### 4 = FEB09
###

colr <- TRUE
if(colr==T){
	flname.pdf <- paste0(the_prefix,"figs/figureA2_favor-by-insurance-by-time-smoothed-color-09112018-wrh.pdf")
}else{
	flname.pdf <- paste0(the_prefix,"figs/favor-by-insurance-by-time-smoothed-gray-09112018-wrh.pdf")
}

pdf(flname.pdf,width=10, height=3.75)

par(mfcol=c(1,2), mar=c(4,6,3,1))

gry.colr <- rep(gray(0.6), 5)

y.lower <- 0.25
y.higher <- 0.75

cex.txt.labels <- 1
cex.axt.labels <- 1.5

sdat$FAVORB <- 1*(sdat$FAVOR > 2.5)
sout <- summaryBy(FAVORB ~ INSURANCETYPE*DATN,FUN=c(mean.na),data=sdat)
sout2 <- summaryBy(FAVORB ~ MARKET*DATN,FUN=c(mean.na),data=subset(sdat, !is.na(MARKET) ))

smout <- loess(sout$FAVORB.mean.na[sout$INSURANCETYPE=="Employer"] ~ sout$DATN[sout$INSURANCETYPE=="Employer"],span=.2)
pout <- predict(smout,sout$DATN[sout$INSURANCETYPE=="Employer"])
plot(y=pout,x=sout$DATN[sout$INSURANCETYPE=="Employer"],type="l",pch=16,
	ylim=c(y.lower,0.9),xlim=c(10,130),xlab="Year",ylab="Percent Favorable on ACA\n",xaxt="n",cex.main=1.5,cex.lab=1.5,yaxt="n",cex=.5,lwd=4, bty="n") #, mgp=c(3.5,1,0)

text("Employer-\n Provided",x=120,y=pout[length(pout)]-.075,col="black",cex=cex.txt.labels)

ss <- seq(from=1,to=120,by=12)
lbs <- c("09","10","11","12","13","14","15","16","17","18")
axis(lty=0,line=NA,tick="no",at=c(ss),labels=lbs,side=1,cex.lab=1.8,cex=cex.axt.labels,cex.axis=cex.axt.labels)
axis(lty=0,line=NA,tick="no",at=c(0,.25,.5,.75,1),labels=c("0","25","50","75","100"),side=2,cex.lab=1.5,cex=cex.axt.labels,cex.axis=cex.axt.labels, las=2)


par(new=T)

smout2 <- loess(sout$FAVORB.mean.na[sout$INSURANCETYPE=="Medicaid"] ~ sout$DATN[sout$INSURANCETYPE=="Medicaid"],span=.15)
pout2 <- predict(smout2,sout$DATN[sout$INSURANCETYPE=="Medicaid"])
if(colr==T){
	lines(y=pout2,x=sout$DATN[sout$INSURANCETYPE=="Medicaid"],type="l",pch=18,
             ylim=c(y.lower,y.higher),xlim=c(-10,130),xlab="",ylab="",xaxt="n",col="blue",yaxt="n",cex=.5,lwd=4,
             bty="n"
             )
        abline(h=c(0,1))
	text("Medicaid",x=120,y=pout2[length(pout2)],col="blue",cex=cex.txt.labels)


}else{
		lines(y=pout2,x=sout$DATN[sout$INSURANCETYPE=="Medicaid"],type="l",pch=18,
                     ylim=c(y.lower,y.higher),xlim=c(-10,130),xlab="",ylab="",xaxt="n",col=gry.colr[2],yaxt="n",cex=.5,lwd=4,
                     bty="n")
                abline(h=c(0,1))
		text("Medicaid",x=120,y=pout2[length(pout2)],col=gry.colr[2],cex=cex.txt.labels)
}

par(new=T)

smout4 <- loess(sout$FAVORB.mean.na[sout$INSURANCETYPE=="Medicare"] ~ sout$DATN[sout$INSURANCETYPE=="Medicare"],span=.15)
pout4 <- predict(smout4,sout$DATN[sout$INSURANCETYPE=="Medicare"])
lines(y=pout4,x=sout$DATN[sout$INSURANCETYPE=="Medicare"],type="l",pch=20,
	ylim=c(y.lower,y.higher),xlim=c(-10,130),xlab="",ylab="",xaxt="n",col=gry.colr[3],yaxt="n",cex=.5,lwd=4, bty="n")
abline(h=c(0,1))
text("Medicare",x=120,y=pout4[length(pout4)],col=gry.colr[3],cex=cex.txt.labels)

abline(v=12*5+1,lty=2)
mtext("Exchanges Open",at=12*5)



smout3 <- loess(sout$FAVORB.mean.na[sout$INSURANCETYPE=="None"] ~ sout$DATN[sout$INSURANCETYPE=="None"],span=.15)
pout3 <- predict(smout3,sout$DATN[sout$INSURANCETYPE=="None"])
plot(y=pout3,x=sout$DATN[sout$INSURANCETYPE=="None"],type="l",pch=15,
	ylim=c(y.lower,0.9),xlim=c(10,130),xaxt="n",col=gry.colr[3],
    ,xlab="Year",ylab="",xaxt="n",main="",cex.main=1.5,cex.lab=1.5,yaxt="n",cex=.5,lwd=4, bty="n")
axis(lty=0,line=NA,tick="no",at=c(0,.25,.5,.75,1),labels=c("0","25","50","75","100"),side=2,cex.lab=1.5,cex=cex.axt.labels,cex.axis=cex.axt.labels, las=2)
abline(h=c(0,1))
ss <- seq(from=1,to=120,by=12)
lbs <- c("09","10","11","12","13","14","15","16","17","18")

axis(lty=0,line=NA,tick="no",at=c(ss),labels=lbs,side=1,cex.lab=1.5,cex=cex.axt.labels,cex.axis=cex.axt.labels)

text("Uninsured",x=120,y=pout3[length(pout3)],col=gry.colr[3],cex=cex.txt.labels)

par(new=T)

smout1 <- loess(sout$FAVORB.mean.na[sout$INSURANCETYPE=="Purchased"] ~ sout$DATN[sout$INSURANCETYPE=="Purchased"],span=.15)
pout1 <- predict(smout1,sout$DATN[sout$INSURANCETYPE=="Purchased"])
if(colr==T){
	lines(y=pout1,x=sout$DATN[sout$INSURANCETYPE=="Purchased"],type="l",pch=17,
		ylim=c(y.lower,y.higher),xlim=c(-10,130),xlab="",ylab="",xaxt="n",col="orange4",yaxt="n",cex=.5,lwd=4, bty="n")
abline(h=c(0,1))
	text("Purchased",x=120,y=pout1[length(pout1)],col="orange4",cex=cex.txt.labels)

}else{
	lines(y=pout1,x=sout$DATN[sout$INSURANCETYPE=="Purchased"],type="l",pch=17,
		ylim=c(y.lower,y.higher),xlim=c(-10,130),xlab="",ylab="",xaxt="n",col=gry.colr[1],yaxt="n",cex=.5,lwd=4, bty="n")
abline(h=c(0,1))
	text("Purchased",x=120,y=pout1[length(pout1)],col=gry.colr[1],cex=cex.txt.labels)
}


smout1 <- loess(sout2$FAVORB.mean.na[sout2$MARKET==1] ~ sout2$DATN[sout2$MARKET==1],span=.5)
pout1 <- predict(smout1,sout2$DATN[sout2$MARKET==1])
if(colr==T){
	lines(y=pout1,x=sout2$DATN[sout2$MARKET==1],type="l",pch=17,
		ylim=c(y.lower,y.higher),xlim=c(-10,130),xlab="",ylab="",xaxt="n",col="#7b3294",yaxt="n",cex=.5,lwd=4, bty="n")
abline(h=c(0,1))
	text("Used\nExchanges",x=120,y=pout1[length(pout1)]-0.1,col="#7b3294",cex=cex.txt.labels)

}else{
	lines(y=pout1,x=sout2$DATN[sout2$MARKET==1],type="l",pch=17,
		ylim=c(y.lower,y.higher),xlim=c(-10,130),xlab="",ylab="",xaxt="n",col=gry.colr[1],yaxt="n",cex=.5,lwd=4, bty="n")
abline(h=c(0,1))
	text("Used\nExchanges",x=120,y=pout1[length(pout1)]-0.1,col=gry.colr[1],cex=cex.txt.labels)
}

abline(v=12*5+1,lty=2)
mtext("Exchanges Open",at=12*5)

dev.off()
